package com.example.rbacmanage.config;

import com.example.rbacmanage.interceptor.DefinedInterceptor;
import com.example.rbacmanage.interceptor.AppInterceptor;
import com.example.rbacmanage.property.DefinedProperty;
import com.example.rbacmanage.resolver.app.CurrentUserArgumentResolver;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;


@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private DefinedProperty definedProperty;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/profile/**").addResourceLocations("file:" +definedProperty.getUploadProfile());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new DefinedInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/app/**","/profile/**","/system/user/login","/system/user/register","/background/menuorder/refund_notify_url/**");
       /* registry.addInterceptor(new AppInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/background/**","/system/**","/profile/**","/app/user/login","/app/user/register");*/
    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new CurrentUserArgumentResolver());
    }
}
